package com.rstrulak.utils;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.context.SecurityContextHolder;

import com.rstrulak.model.User;

public class JSFUtils {
	public static Flash flashScope() {
		return (FacesContext.getCurrentInstance().getExternalContext().getFlash());
	}

	public static void redirect(String url) throws ServletException, IOException {
		getExternalContext().redirect(
				new StringBuilder().append(((HttpServletRequest) getExternalContext().getRequest()).getContextPath())
						.append(url).toString());
	}

	public static ExternalContext getExternalContext() {
		return FacesContext.getCurrentInstance().getExternalContext();
	}

	public static User getLoggedUser() {
		User user = (User) (SecurityContextHolder.getContext().getAuthentication().getPrincipal());
		return user;
	}

	public static HttpSession getSession() {
		return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(Boolean.TRUE);
	}

	public static void reloadFlash() {

		Long _contextId = (Long) JSFUtils.flashScope().get(Constants.CONTEXT_ID);
		JSFUtils.flashScope().put(Constants.CONTEXT_ID, _contextId);

		Long _projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, _projectId);

		Long _taskId = (Long) JSFUtils.flashScope().get(Constants.TASK_ID);
		JSFUtils.flashScope().put(Constants.TASK_ID, _taskId);

		Long _noteId = (Long) JSFUtils.flashScope().get(Constants.NOTE_ID);
		JSFUtils.flashScope().put(Constants.NOTE_ID, _noteId);

	}

	public static void setSessionParams() {
		Long _contextId = (Long) JSFUtils.flashScope().get(Constants.CONTEXT_ID);
		JSFUtils.flashScope().put(Constants.CONTEXT_ID, _contextId);

		Long _projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, _projectId);

		Long _taskId = (Long) JSFUtils.flashScope().get(Constants.TASK_ID);
		JSFUtils.flashScope().put(Constants.TASK_ID, _taskId);

		Long _noteId = (Long) JSFUtils.flashScope().get(Constants.NOTE_ID);
		JSFUtils.flashScope().put(Constants.NOTE_ID, _noteId);

		getSession().setAttribute(Constants.CONTEXT_ID, _contextId);
		getSession().setAttribute(Constants.PROJECT_ID, _projectId);
		getSession().setAttribute(Constants.TASK_ID, _taskId);
		getSession().setAttribute(Constants.NOTE_ID, _noteId);
	}

	public static void setFlashFromSession() {
		JSFUtils.flashScope().put(Constants.CONTEXT_ID, getSession().getAttribute(Constants.CONTEXT_ID));
		JSFUtils.flashScope().put(Constants.PROJECT_ID, getSession().getAttribute(Constants.PROJECT_ID));
		JSFUtils.flashScope().put(Constants.TASK_ID, getSession().getAttribute(Constants.TASK_ID));
		JSFUtils.flashScope().put(Constants.NOTE_ID, getSession().getAttribute(Constants.NOTE_ID));

	}

}
